package cn.dckeji.dcssm.task;

import cn.dckeji.dcssm.common.utils.JedisUtil;
import cn.dckeji.dcssm.entity.His;
import cn.dckeji.dcssm.entity.Park;
import cn.dckeji.dcssm.service.HisService;
import cn.dckeji.dcssm.service.ParkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 * @author 41014 
 *
 */
@Component
public class UserBalanceTask {

	@Autowired
	private ParkService parkService;

	@Autowired
	private HisService hisService;

	private static Logger log = LoggerFactory.getLogger(UserBalanceTask.class);
	//@Scheduled(cron = "0 50 23 * * ?")
	@Scheduled(cron = "0/5 * * * * ? ") // 间隔5秒执行
	public void taskCycle() throws Exception {
		//查询全部车位列表
		List<Park> parkList = parkService.list(new QueryWrapper<Park>().eq("deleteFlag",0).orderByAsc("gmt_create"));
		if(parkList.size()>0){
			parkList.forEach(Park->{
				String parkNo = Park.getParkNo();
				String userId = JedisUtil.get(parkNo);
				if(StringUtils.isNotBlank(userId)){
					String parkId = Park.getParkId();
					//判断是否存储过了
					QueryWrapper<His> en = new QueryWrapper<>();
					en.eq("userId",userId);
					en.eq("parkId",parkId);
					int count = hisService.count(en);
					if(count==0){
						His his = new His();
						his.setGmtCreate(LocalDateTime.now());
						his.setParkId(parkId);
						his.setUserId(userId);
						hisService.save(his);

						Park.setUserId(userId);
						parkService.updateById(Park);
					}
				}
			});
		}

		//redis查询是否为空  为空说明没有被抢购  不为空保存到数据库  跟新两张表                        his system_park

	}
}

/*注意事项：

1、spring的@Scheduled注解  需要写在实现方法上

2、 定时器的任务方法不能有返回值（如果有返回值，spring初始化的时候会告诉你有个错误、需要设定一个proxytargetclass的某个值为true）

3、实现类上要有组件的注解@Component
*/